package com.sarmentegui.laPerra.filter;

import java.io.IOException;
import java.util.ArrayList;
//import java.util.ResourceBundle;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import com.sarmentegui.laPerra.utils.ContextKeys;


public class HeaderFilter implements Filter{
	FilterConfig config;
	Logger logger = Logger.getLogger("HeaderFilter.class");
	ArrayList<String>accionesAContar;
	
	public HeaderFilter() {
		
		accionesAContar=new ArrayList<String>();
		//inicio
		accionesAContar.add("/inicio.do");
		//somos
		accionesAContar.add("/somos.do");//
		accionesAContar.add("/directora.do");//
		accionesAContar.add("/colaboradores.do");//
		//eventos
		accionesAContar.add("/eventos.do");//
		accionesAContar.add("/eventoInfantil.do");//
		accionesAContar.add("/eventoInfantilCumple.do");
		accionesAContar.add("/eventoInfantilAnimacion.do");
		accionesAContar.add("/eventoInfantilDeco.do");
		
		accionesAContar.add("/eventoTuristica.do");//
		accionesAContar.add("/eventoAdultos.do");///
		accionesAContar.add("/eventoPopulares.do");///
		//espectaculos
		accionesAContar.add("/espectaculos.do");//
		accionesAContar.add("/espectaculoPlaneta.do");//
		accionesAContar.add("/espectaculoYeMasCaro.do");
		accionesAContar.add("/espectaculoFutbol.do");
		accionesAContar.add("/espectaculoCabrales.do");
		accionesAContar.add("/espectaculoAsturias.do");
		accionesAContar.add("/espectaculoKennedy.do");
		accionesAContar.add("/servicios.do");//
		
		//galeria
		accionesAContar.add("/galeria.do");//
		
		//contacto
		accionesAContar.add("/contacto.do");//
		accionesAContar.add("/enviarContacto.do");//
	}

	public void destroy() {

	}

	public void doFilter(ServletRequest req, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		
		HttpServletRequest request = (HttpServletRequest) req;
		String action = request.getServletPath();
		logger.info(action);
		if (accionesContar(action)){
			trataAction(action);
		}
		
		chain.doFilter(request, response);
	}

	private boolean accionesContar(String action) {
		return accionesAContar.contains(action);
	}

	private void trataAction(String action) {
		String header = "";
		if (action.equalsIgnoreCase("/somos.do")){
			header = ContextKeys.HEADER_SOMOS_TEXTO;
		}else if(action.equalsIgnoreCase("/directora.do")){
			header = ContextKeys.HEADER_DIRECTORA_TEXTO;
		}else if(action.equalsIgnoreCase("/colaboradores.do")){
			header = ContextKeys.HEADER_COLABORADORES_TEXTO;
		}else if(action.equalsIgnoreCase("/inicio.do")){
			header = ContextKeys.HEADER_INICIO_TEXTO;
		}else if(action.equalsIgnoreCase("/eventoInfantil.do")){
			header = ContextKeys.HEADER_EVENTO_INFANTIL_TEXTO;
		}else if(action.equalsIgnoreCase("/eventoInfantilCumple.do")){
			header = ContextKeys.HEADER_EVENTO_INFANTIL_CUMPLE_TEXTO;
		}else if(action.equalsIgnoreCase("/eventoInfantilAnimacion.do")){
			header = ContextKeys.HEADER_EVENTO_INFANTIL_ANIMACION_TEXTO;
		}else if(action.equalsIgnoreCase("/eventoInfantilDeco.do")){
			header = ContextKeys.HEADER_EVENTO_INFANTIL_DECORACION_TEXTO;
		}else if(action.equalsIgnoreCase("/eventoTuristica.do")){
			header = ContextKeys.HEADER_EVENTO_TURISTICA_TEXTO;
		}else if(action.equalsIgnoreCase("/eventos.do")){
			header = ContextKeys.HEADER_EVENTOS_TEXTO;
		}else if(action.equalsIgnoreCase("/eventoPopulares.do")){
			header = ContextKeys.HEADER_EVENTO_POPULARES_TEXTO;
		}else if(action.equalsIgnoreCase("/eventoAdultos.do")){
			header = ContextKeys.HEADER_EVENTO_ADULTOS_TEXTO;
		}else if(action.equalsIgnoreCase("/espectaculos.do")){
			header = ContextKeys.HEADER_ESPECTACULOS_TEXTO;
		}else if(action.equalsIgnoreCase("/espectaculoPlaneta.do")){
			header = ContextKeys.HEADER_ESPECTACULOS_TEXTO;
		}else if(action.equalsIgnoreCase("/espectaculoYeMasCaro.do")){
			header = ContextKeys.HEADER_ESPECTACULOS_TEXTO;
		}else if(action.equalsIgnoreCase("/espectaculoFutbol.do")){
			header = ContextKeys.HEADER_ESPECTACULOS_TEXTO;
		}else if(action.equalsIgnoreCase("/espectaculoCabrales.do")){
			header = ContextKeys.HEADER_ESPECTACULOS_TEXTO;
		}else if(action.equalsIgnoreCase("/espectaculoAsturias.do")){
			header = ContextKeys.HEADER_ESPECTACULOS_TEXTO;
		}else if(action.equalsIgnoreCase("/espectaculoKennedy.do")){
			header = ContextKeys.HEADER_ESPECTACULOS_TEXTO;
		}else if(action.equalsIgnoreCase("/galeria.do")){
			header = ContextKeys.HEADER_GALERIA_TEXTO;
		}else if(action.equalsIgnoreCase("/contacto.do")){
			header = ContextKeys.HEADER_CONTACTO_TEXTO;
		}else if(action.equalsIgnoreCase("/enviarContacto.do")){
			header = ContextKeys.HEADER_ENVIAR_CONTACTO_TEXTO;
		}
		
		config.getServletContext().setAttribute(action,
						header);
		desactiva_resto(action);
		logger.info(header);
	}
		

	private void desactiva_resto(String action) {
		for (String accion:accionesAContar){
			if (!action.equalsIgnoreCase(accion)){
				config.getServletContext().setAttribute(accion,
						"");
			}
		}
		
	}

	public void init(FilterConfig fConfig) throws ServletException {
		config = fConfig;
	
		fConfig.getServletContext().setAttribute(
				ContextKeys.HEADER_ESPECTACULOS, "");
//		fConfig.getServletContext().setAttribute(
//				ContextKeys.HEADER_ESPECTACULO_PLANETA, "");
//		fConfig.getServletContext().setAttribute(
//				ContextKeys.HEADER_ESPECTACULO_YEMASCARO, "");
//		EVENTOS
		fConfig.getServletContext().setAttribute(
				ContextKeys.HEADER_EVENTOS, "");
		fConfig.getServletContext().setAttribute(
				ContextKeys.HEADER_EVENTO_INFANTIL, "");
		fConfig.getServletContext().setAttribute(
				ContextKeys.HEADER_EVENTO_INFANTIL_CUMPLE, "");
		fConfig.getServletContext().setAttribute(
				ContextKeys.HEADER_EVENTO_INFANTIL_ANIMACION, "");
		fConfig.getServletContext().setAttribute(
				ContextKeys.HEADER_EVENTO_INFANTIL_DECORACION, "");
		fConfig.getServletContext().setAttribute(
				ContextKeys.HEADER_EVENTO_TURISTICA, "");
		fConfig.getServletContext().setAttribute(
				ContextKeys.HEADER_EVENTO_ADULTOS, "");
		fConfig.getServletContext().setAttribute(
				ContextKeys.HEADER_EVENTO_POPULARES, "");
		
//		SOMOS
		fConfig.getServletContext().setAttribute(
				ContextKeys.HEADER_SOMOS, "");
		fConfig.getServletContext().setAttribute(
				ContextKeys.HEADER_INICIO, "");
		fConfig.getServletContext().setAttribute(
				ContextKeys.HEADER_DIRECTORA, "");
		fConfig.getServletContext().setAttribute(
				ContextKeys.HEADER_COLABORADORES, "");
		fConfig.getServletContext().setAttribute(
				ContextKeys.HEADER_CONTACTO, "");
		fConfig.getServletContext().setAttribute(
				ContextKeys.HEADER_ENVIAR_CONTACTO, "");
		fConfig.getServletContext().setAttribute(
				ContextKeys.HEADER_GALERIA, "");
		
	}

}
